<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Ruby Web Services: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Ruby Study Notes - Best Ruby Guide, Ruby Tutorial" />
<meta name="keywords" content="ruby web services example,ruby webservices,ruby webservices example" />
<meta name="Distribution" content="Global" />
<meta name="author" content="Satish Talim / Original design: Erwin Aligam - ealigam@gmail.com" />
<meta name="copyright" content="Satish Talim 2007 and beyond..." />
<meta name="verify-v1" content="rFu86se+IkbtF+bH8mgJBKwU5HnKaSd8Ghw9umXQOkM=" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Expires" content="0" />
<meta name="revisit-after" content="1 days" />
<link rel="stylesheet" href="/images/NewOrange.css" type="text/css" />
<link rel="stylesheet" href="/images/syntaxhighlighter.css" type="text/css" />
<link rel="icon" type="image/ico" href="/images/favicon.ico" />
<!-- Google Analytics code -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-59044-10']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<!-- Google Analytics code ends -->
</head>

<body>
<!-- wrap starts here -->
<div id="wrap">

    <div id="header">

        <h1 id="logo">Ruby<span class="orange">Learning.github.io</span></h1>
        <h2 id="slogan">Ruby helps programmers have more fun!</h2>

    </div>

    <div id="menu">
        <ul>
            <li><a href="/" title="Home page for rubylearning.github.io">Home</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Get started Learning Ruby here...">Tutorial</a></li>
            <li><a href="/download/downloads.html" title="Download this tutorial as an eBook">Downloads</a></li>
            <li><a href="/other/testimonials.html" title="People around the world who benefited from this site">Testimonials</a></li>
            <li><a href="/other/ruby_gurus.html" title="Interviews I took of Ruby Gurus around the world">Gurus</a></li>
            <li><a href="/other/certification.html" title="Get certified in Ruby">Certification</a></li>
            <li><a href="/satishtalim/ruby_guide.html" title="Ruby Guide, Mentor">Mentor</a></li>
            <li><a href="/other/ruby_video_search.html" title="Watch videos on Ruby/Rails">Videos</a></li>
            <li><a href="/blog/" title="Ruby blog of Learning Ruby site">Blog</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Online Ruby Course">Online Course</a></li>
            <li><a href="/satishtalim/about.html" title="Information about Satish Talim">About</a></li>
        </ul>
    </div>

    <!-- content-wrap starts here -->
    <div id="content-wrap">

            <div id="main">

                <div id="main-inner"><a name="TemplateInfo"></a>
                <h1>Ruby Web Services</h1>

                <!--
                <div class="container">
                  <div class="adsense">
                  <strong>Note: This topic has been re-written on 25th Nov. 2007.</strong>
                  </div>
                </div>
                -->
                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/ruby_smtp.html">&lt;SMTP Class | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/ruby_internet_services.html">Ruby Internet Services &gt;</a>
                  </strong>
                </p>

                <p>The basic point of distributed programming is to let computers communicate between themselves. The simplest and most common form of distributed programming is the web service. Web services work on top of HTTP: they generally involve sending an HTTP request to a certain URL (possibly including an XML document), and getting a response in the form of another XML document. Rather than showing this document to an end user the way a web browser would, the web service parses the XML response document and does something with it. Web services work well when there's a server with some interesting data and many clients who want it.</p>

                <p>From a high-level view, web service implementations can be broken down into two categories: servers and clients. Most web services are based on one of three architectures: Representational State Transfer (REST), Simple Object Access Protocol (SOAP), or Extensible Markup Language Remote Procedural Calls (XML-RPC). REST is HTTP; XML-RPC and SOAP are protocols that run on top of HTTP. XML-RPC isn't used much nowadays.</p>

                <h3>Writing a RubyLearning SOAP server and client</h3>

                <p>Ruby's built-in SOAP library (implements both the client and server sides of the SOAP protocol) handles the low-level details for you, leaving you free to focus on using the results in your program. To write the SOAP service, we use Hiroshi Nakamura's <strong>soap4r</strong> library. It is part of the Ruby standard library and implements SOAP version 1.1 (the <strong>soap4r</strong> library is not included in Ruby 1.9).</p>

                <p>Let's say that RubyLearning wants to host a SOAP-based web service (we are going to build a trivial one just to show how it is done) using a standalone server (<strong>SOAP::Standalone Server</strong> that is, not as part of a Rails application).</p>

                <p>Building our own SOAP server (program <strong>p072soapserver.rb</strong>) really requires only three simple steps:</p>

                <p>Subclass the <strong>SOAP::Standalone Server</strong> class. In the <strong>initialize</strong> method, register the methods you want to expose and the arguments they should take. Here we expose a method <strong>sayhelloto</strong> that expects one parameter, <strong>username</strong>:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                require 'logger'
                require 'soap/rpc/standaloneServer'
                class MyServer &lt; SOAP::RPC::StandaloneServer
                  def initialize(* args)
                    super
                    add_method(self, 'sayhelloto', 'username')
                    # create a log file
                    @log = Logger.new("soapserver.log", 5, 10*1024)
                  end
                  def sayhelloto(username)
                    t = Time.now
                    @log.info("#{username} logged on #{t}")
                    "Hello, #{username} on #{t}."
                  end
                end

                server = MyServer.new('RubyLearningServer','urn:mySoapServer','localhost',12321)
                trap('INT') {server.shutdown}
                server.start
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>The above example runs on port 12321 on localhost (you could host it on your own domain too). It's name is 'RubyLearningServer' and its namespace is 'urn:mySoapServer'. We've now built a complete SOAP server. It uses the SOAP StandaloneServer and hosts one simple <strong>sayhelloto</strong> method that can be accessed at http://127.0.0.1:12321/ with a namespace of "urn:mySoapServer". We trap interrupt signals so that we can stop our server from the command line.</p>

                <p>To test your service, start the above server in one Ruby session and then use the simple script (<strong>p073prclient.rb</strong>) below in another Ruby session to call the method it exposes:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                # p073prclient.rb
                require 'soap/rpc/driver'
                driver = SOAP::RPC::Driver.new('http://127.0.0.1:12321/', 'urn:mySoapServer')
                driver.add_method('sayhelloto', 'username')
                puts driver.sayhelloto('RubyLearning')
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>The output is:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                >ruby p073prclient.rb
                Hello, RubyLearning on Sun Nov 25 14:57:54 +0530 2007.
                >Exit code: 0
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p style="background-color: #FAFAFA; padding: 5px; margin-top: 20px; font-size: 65%;"><strong>Note</strong>: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various <span style="font-weight: bold;" title="Click Gurus on the menu above">Ruby Gurus</span>), my acknowledgment and thanks to all of them.</p>

                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/ruby_smtp.html">&lt;SMTP Class | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/ruby_internet_services.html">Ruby Internet Services &gt;</a>
                  </strong>
                </p>

            </div>
            <!-- main inner ends here -->
        </div>

            <div id="rightbar">

            </div>

    <!-- content-wrap ends here -->
    </div>

<!-- wrap ends here -->
</div>

<!-- footer starts here -->
<div id="footer">
    <!-- CHANGE THE FOOTER -->
    <p>&copy; 2006-2021 <strong>rubylearning.github.io - A Ruby Tutorial</strong>&nbsp;&nbsp;Page Updated: 5th Jan. 2021 | Design: <a href="mailto:ealigam@gmail.com">Erwin Aligam</a> | Valid: <a href="http://validator.w3.org/check/referer">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/">Home</a> | <a href="/privacy.html">Privacy</a> | <a href="/sitemap.html">Sitemap</a></p>
</div>

<!-- footer ends here -->

<!-- SyntaxHighlighter code -->
<script src="/js/shCore.js" type="text/javascript"></script>
<script src="/js/shBrushRuby.js" type="text/javascript"></script>
<script type="text/javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!-- SyntaxHighlighter code -->

</body>
</html>
